﻿using Furion.CMS.Application.Dto;
using Furion.CMS.Domain;
using Mapster;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Furion.CMS.Application.Profile
{
    public class SettingsProfile : IRegister
    {
        public void Register(TypeAdapterConfig config)
        {
            config.ForType<SiteSetting, SiteSettingsDto>()
                .Ignore(p => p.StorageModeList)
                .AfterMapping((src, dest) =>
                {
                    if (src.ObjectStorageParam != null)
                    {
                        dest.StorageHostUrl = src.ObjectStorageParam.Url;
                        dest.StorageAccessKey = src.ObjectStorageParam.KeyId;
                        dest.StorageAccessKeySecret = src.ObjectStorageParam.KeySecret;
                    }
                });
            config.ForType<SiteSetting, SiteInformationDto>();
            config.ForType<UpdateSiteSettingsDto, SiteSetting>()
                .AfterMapping((src, dest) =>
                {
                    dest.ObjectStorageParam = new ObjectStorageParams
                    {
                        Url = dest.ObjectStorageMode == Core.StorageMode.Local ? "" : src.StorageHostUrl,
                        KeyId = dest.ObjectStorageMode == Core.StorageMode.Local ? "" : src.StorageAccessKey,
                        KeySecret = dest.ObjectStorageMode == Core.StorageMode.Local ? "" : src.StorageAccessKeySecret
                    };
                });

            #region Dictionary
            config.ForType<DataDictionary, DataDictionaryDto>()
                .Map(dest => dest.Category, src => src.Category.Name);
            config.ForType<DataDictionaryCategory, DataDictionaryCategoryDto>();

            config.ForType<DataDictionary, DataDictionaryDetailDto>();
            config.ForType<DataDictionaryCategory, DataDictionaryCategoryDetailDto>();

            config.ForType<CreateDataDictionaryDto, DataDictionary>();
            config.ForType<InputDataDictionaryCategoryDto, DataDictionaryCategory>();
            #endregion
        }
    }
}
